package com.umakr.ax.core.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.sql.DataSource;

/**
 *
 * @author gx
 * @date 2017/4/14
 */
@ConfigurationProperties(prefix = "base.db")
public class DruidDataSourceProperties {
    Integer initialSize;
    Integer minIdle;
    Long maxWait;
    Integer maxActive;
    Long minEvictableIdleTimeMillis;
    String validationQuery;
    Boolean testOnBorrow;
    Boolean testOnReturn;
    Boolean testWhileIdle;
    public Integer getInitialSize() {
        return initialSize;
    }

    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }

    public Long getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(Long maxWait) {
        this.maxWait = maxWait;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    public Long getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(Boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public Boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(Boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public Boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(Boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }


    public void buildDataSource(DataSource dataSource){
        if(dataSource instanceof DruidDataSource){
            DruidDataSource druidDataSource = (DruidDataSource)dataSource;
            if(initialSize!=null) {
                druidDataSource.setInitialSize(initialSize);
            }
            if(minIdle!=null) {
                druidDataSource.setMinIdle(minIdle);
            }
            if(maxWait!=null) {
                druidDataSource.setMaxWait(maxWait);
            }
            if(maxActive!=null) {
                druidDataSource.setMaxActive(maxActive);
            }
            if(minEvictableIdleTimeMillis!=null) {
                druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            }
            if(testOnBorrow!=null) {
                druidDataSource.setTestOnBorrow(testOnBorrow);
            }
            if(testOnReturn!=null) {
                druidDataSource.setTestOnReturn(testOnReturn);
            }
            if(testWhileIdle!=null) {
                druidDataSource.setTestWhileIdle(testWhileIdle);
            }
            if(validationQuery!=null) {
                druidDataSource.setValidationQuery(validationQuery);
            }
        }else{
            throw new RuntimeException("Only DruidDataSource you can use!");
        }

    }
}
